package com.example.socket_client_demo;

import android.app.Activity;
import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
import android.util.Log;
import com.sun.deploy.util.SystemUtils;
import com.uwantsoft.goeasy.client.goeasyclient.GoEasy;
import com.uwantsoft.goeasy.client.goeasyclient.listener.GoEasyError;
import com.uwantsoft.goeasy.client.goeasyclient.listener.SubscribeListener;
import com.uwantsoft.goeasy.client.goeasyclient.message.GoEasyClientMessage;
//import com.uwantsoft.goeasy.client.goeasyclient.notification.CustomerNotificationBuilder;
//import com.uwantsoft.goeasy.client.goeasyclient.notification.GoEasyNotificationManager;

/**
 * @author Chuanbao
 */
public class MyService extends Service {
    private GoEasy goEasy;
    private String TAG = ".MyService";
    private MyBinder myBinder = new MyBinder();
    private MyActivity myActivity;

    @Override
    public IBinder onBind(Intent intent) {
        Log.i(TAG, "MyService onBind");
        return myBinder;
    }

    @Override
    public void onCreate() {
//        Log.i(TAG, "MyService onCreate()");
//        goEasy = new GoEasy("ba821151-e043-4dfb-a954-c73744c8d323",getApplication());
//        goEasy.subscribe("demo_channel", new SubscribeListener() {
//            public void onSuccess() {
//                Log.i(TAG, "subscribe success.");
//                myActivity.runOnUiThread(new Runnable() {
//                    public void run() {
//                        myActivity.messageView.append("Subscribe channel successfully.");
//                    }
//                });
//            }
//
//            public void onFailed(final GoEasyError error) {
//                Log.i(TAG, "subscribe failed with error:"+error.getCode()+":"+error.getContent());
//            }
//
//            public void onMessage(final GoEasyClientMessage clientMessage) {
//                Log.i(TAG, "receive message:" + clientMessage.getContent()+"\n");
//                notification(clientMessage);
//                myActivity.runOnUiThread(new Runnable() {
//                    public void run() {
//                        myActivity.messageView.append("Receive message:"+clientMessage.getContent()+"\n");
//                    }
//                });
//            }
//        });
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Log.i(TAG, "MyService onStartCommand");
        return super.onStartCommand(intent,flags,startId);
    }

    @Override
    public void onDestroy() {
        Log.i(TAG,"MyService onDestroy()");
    }

    @Override
    public boolean onUnbind(Intent intent) {
        Log.i(TAG, "MyService onUnbind()");
        return super.onUnbind(intent);
    }

    @Override
    public void onRebind(Intent intent) {
        Log.i(TAG, "MyService onRebind()");
    }

    public GoEasy socketClient() {
        return goEasy;
    }

    private void notification(GoEasyClientMessage clientMessage){
//        GoEasyNotificationManager notificationManager = new GoEasyNotificationManager();
//        CustomerNotificationBuilder builder = new CustomerNotificationBuilder(R.drawable.ic_launcher,"A new message","New message",clientMessage.getContent());
//        notificationManager.setBuilder(builder);
//        notificationManager.notification(myActivity);
    }

    class MyBinder extends Binder{
        public MyService getMyService(){
            return MyService.this;
        }
    }

    public void setMyActivity(MyActivity myActivity) {
        this.myActivity = myActivity;
    }
}
